import VueToast from './previewImage.vue';

export function previewImage(options: { [key: string]: any } = {}) {
  //加载 Vue
  const Vue = require('vue');
  if (typeof options === 'string') {
    options = { url: options };
  }
  // 检查 Vue 版本
  if (Vue.version.startsWith('2.')) {
    // Vue 2 逻辑
    const ImgComponent = Vue.extend(VueToast);
    const instance = new ImgComponent({
      data() {
        return options;
      },
    });
    instance.$mount();
    document.body.appendChild(instance.$el);
    instance.show();
    return instance;
  } else {
    // Vue 3 逻辑
    const { createApp } = Vue;
    const app = createApp(VueToast);
    const container = document.createElement('div');
    const instance: any = app.mount(container);
    document.body.appendChild(container);

    Object.keys(options).forEach((key: string) => {
      instance[key] = options[key];
    });
    instance.show();
    return instance;
  }
}
